using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

using DevExpress.Xpo;
using DevExpress.Data.Filtering;

using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.ExpressApp.DC;

namespace WaTP.Module.BusinessObjects
{
    //[DefaultClassOptions]
    public class ExportOperation {
        public const string ExportOperationName = "Export";
    }

    [XafDisplayName("Role")]
    public class CustomSecurityRole : SecuritySystemRole
    {
        public CustomSecurityRole(Session session)
            : base(session)
        {
            // This constructor is used when an object is loaded from a persistent storage.
            // Do not place any code here or place it only when the IsLoading property is false:
            // if (!IsLoading){
            //    It is now OK to place your initialization code here.
            // }
            // or as an alternative, move your initialization code into the AfterConstruction method.
        }
        protected override IEnumerable<IOperationPermission> GetPermissionsCore()
        {
            List<IOperationPermission> permissions = new List<IOperationPermission>(base.GetPermissionsCore());
            foreach(SecuritySystemTypePermissionObject persistentPermission in TypePermissions){
                CustomTypePermissionObject customPermission = persistentPermission as CustomTypePermissionObject;
                if (customPermission != null && customPermission.AllowExport) {
                    permissions.Add(new TypeOperationPermission(persistentPermission.TargetType, ExportOperation.ExportOperationName));
                }
            }
            return permissions;
        }
        public override void AfterConstruction()
        {
            base.AfterConstruction();
            // Place here your initialization code.
        }
    }
    [XafDisplayName("Type Operation Permissions")]
    public class CustomTypePermissionObject : SecuritySystemTypePermissionObject {
        public CustomTypePermissionObject(Session session) : base(session) { }
        [XafDisplayName("Export")]
        public bool AllowExport {
            get { return GetPropertyValue<bool>("AllowExport"); }
            set { SetPropertyValue<bool>("AllowExport", value); }
        }
    }
    
}
